package com.shike.hq.controller;

import com.shike.hq.entity.Admin;
import com.shike.hq.entity.Area;
import com.shike.hq.mapper.AdminMapper;
import com.shike.hq.service.AdminService;
import com.shike.hq.service.AreaService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Array;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * The type Admins controller.
 *
 * @author Saw96x
 * @date 21 /05/03 - 09:05
 */
@Controller
@RequestMapping("/base")
public class AdminsController {

  private static final String REDIRECT = "redirect:/base/admins";

  /**
   * The Admin service.
   */
  @Autowired
  AdminService adminService;

  @Autowired
  AreaService areaService;

  /**
   * The Admin mapper.
   */
  @Autowired
  AdminMapper adminMapper;

  /**
   * 页面映射.
   *
   * @param model model类型
   * @return 页面路径字符串 string
   */
  //TODO:改为使用区域id进行子查询
  @GetMapping("/admins")
  public String admin(Model model, HttpSession session) {
    // 修改list()为selectAdmin()
    List<Admin> list = adminService.selectAdmin();
    model.addAttribute("admins", list);
    Admin admin = (Admin) session.getAttribute("admin");
    long areaId = admin.getAreaId();
    ArrayList<Area> list1 = areaService.selectAllAreaById(areaId);
    model.addAttribute("areas", list1);
    return "base/admins";
  }


  /**
   * 通过id删除管理员
   *
   * @param id         管理员id
   * @param attributes model类型
   * @return 页面路径字符串 string
   */
  @GetMapping("/admins/delete")
  public String deleteById(@RequestParam("id") long id, RedirectAttributes attributes) {
    try {
      adminService.removeById(id);
      attributes.addFlashAttribute("msg", "删除成功！");
    } catch (Exception e) {
      attributes.addFlashAttribute("msg", "删除失败!");
    }
    return REDIRECT;
  }

  /**
   * 通过id列表批量删除管理员
   *
   * @param idList     the id list
   * @param attributes model类型
   * @return 页面路径字符串 string
   */
  @GetMapping("/admins/deleteAdmins")
  public String deleteAdmins(@RequestParam("idList") ArrayList<Long> idList, RedirectAttributes attributes) {
    try {
      adminService.removeByIds(idList);
      attributes.addFlashAttribute("msg", "删除成功！");
    } catch (Exception e) {
      attributes.addFlashAttribute("msg", "删除失败!");
    }
    return REDIRECT;
  }


  /**
   * 通过id更新管理员
   *
   * @param admin 添加的管理员
   * @return 页面路径字符串 string
   */
  @PostMapping("/admins/update")
  public String updateAdmin(Admin admin, RedirectAttributes attributes,HttpSession session) {
    try {
      adminService.updateById(admin);
      attributes.addFlashAttribute("msg","修改成功！");
    } catch (Exception e) {
      attributes.addFlashAttribute("msg","修改失败！");
    }
    return "redirect:/base/admins";
  }


  /**
   * 添加管理员
   *
   * @param admin 添加的管理员
   * @return 页面路径字符串 string
   */
  @PostMapping("/admins/addAdmin")
  public String addAdmin(Admin admin, RedirectAttributes attributes) {
    admin.setCreateTime(new Timestamp(System.currentTimeMillis()));
    try {
      adminMapper.insert(admin);
      attributes.addFlashAttribute("msg", "添加成功！");
    } catch (Exception e) {
      attributes.addFlashAttribute("msg", "添加失败！");
    }
    return "redirect:/base/admins";
  }

  /**
   * 指定区域进行页面映射.
   *
   * @param model model类型
   * @return 页面路径字符串 string
   */
  @PostMapping("/admins/showRefresh")
  public String showRefresh(Model model, @RequestParam("ParentId") long ParentId) {
    ArrayList<Admin> list = adminService.selectAdminsByAreaId(ParentId);
    model.addAttribute("admins", list);
    ArrayList<Area> list1 = areaService.selectAllAreaById(ParentId);
    model.addAttribute("areas", list1);
    return "/base/admins::showRefresh";
  }
}
